/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exterminator.game.controls;

import com.jme3.app.state.AbstractAppState;
import com.jme3.material.RenderState;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import emitter.Emitter;
import emitter.EmitterMesh;
import emitter.Interpolation;
import emitter.influencers.AlphaInfluencer;
import emitter.influencers.ColorInfluencer;
import emitter.influencers.GravityInfluencer;
import emitter.influencers.ImpulseInfluencer;
import emitter.influencers.RotationInfluencer;
import emitter.influencers.SizeInfluencer;
import exterminator.Main;
import exterminator.utils.AudioManager.FXKey;
import tonegod.gui.core.Screen;

/**
 *
 * @author t0neg0d
 */
public class Hose extends AbstractAppState {
	Main main;
	Screen screen;
	Node emitterModelNode;
	Mesh emitterModelMesh;
	Emitter eHose;
	Quaternion q = new Quaternion();
	
	public Hose(Main main) {
		this.main = main;
		this.screen = main.getScreen();
		
		addTestEmitters();
	}
	
	
	
	private void addTestEmitters() {
		emitterModelNode = (Node)main.getAssetManager().loadModel("Models/EmitterShapes/Circle.j3o");
		emitterModelMesh = ((Geometry)emitterModelNode.getChild(0)).getMesh();
		
		eHose = new Emitter();
		eHose.setName("ePhysics");
		eHose.setMaxParticles(800);
		eHose.addInfluencers(
			new GravityInfluencer(),
			new ColorInfluencer(),
			new AlphaInfluencer(),
			new SizeInfluencer(),
			new RotationInfluencer(),
			new ImpulseInfluencer()
		);
		eHose.setShape(emitterModelMesh);
		eHose.setBillboardMode(Emitter.BillboardMode.Velocity_Z_Up_Y_Left);
		eHose.setSprite("Textures/glow.png");
		eHose.setForceMinMax(8.0f, 12.0f);
		eHose.setLifeMinMax(2.0f,2.2f);
		eHose.setEmissionsPerSecond(0);
		eHose.setParticlesPerEmission(2);
		eHose.setEmitterTestMode(false, false);
		eHose.setUseRandomEmissionPoint(true);
		eHose.setParticlesFollowEmitter(false);
		eHose.setUseVelocityStretching(true);
		eHose.setDirectionType(EmitterMesh.DirectionType.Normal);
		
		eHose.getInfluencer(GravityInfluencer.class).setGravity(0,16,0);
		
		eHose.getInfluencer(SizeInfluencer.class).addSize(new Vector3f(0.06f,0.06f,0.06f));
		eHose.getInfluencer(SizeInfluencer.class).addSize(new Vector3f(0.1f,0.1f,0.1f));
		eHose.getInfluencer(SizeInfluencer.class).setEnabled(true);
		
		eHose.getInfluencer(ColorInfluencer.class).addColor(ColorRGBA.Blue, Interpolation.linear);
		eHose.getInfluencer(ColorInfluencer.class).addColor(new ColorRGBA(0.1f,0.1f,1f,1f), Interpolation.linear);
		eHose.getInfluencer(ColorInfluencer.class).setUseRandomStartColor(true);
		
		eHose.getInfluencer(AlphaInfluencer.class).addAlpha(1f, Interpolation.exp10Out);
		eHose.getInfluencer(AlphaInfluencer.class).addAlpha(1f);
		eHose.getInfluencer(AlphaInfluencer.class).addAlpha(0.25f, Interpolation.exp5Out);
		
		eHose.getInfluencer(ImpulseInfluencer.class).setMagnitude(0.06f);
		
		eHose.getInfluencer(RotationInfluencer.class).setUseRandomDirection(true);
		eHose.getInfluencer(RotationInfluencer.class).addRotationSpeed(new Vector3f(0,0,3));
		
		eHose.setLocalTranslation(main.getCamera().getLocation().add(1, -1, 0));
		eHose.setLocalScale(0.05f);
		eHose.setLocalRotation(eHose.getLocalRotation().fromAngleAxis(-90*FastMath.DEG_TO_RAD, Vector3f.UNIT_X));
		
		eHose.initialize(main.getAssetManager());
		
	//	eHose.getMaterial().getAdditionalRenderState().setBlendMode(RenderState.BlendMode.AlphaAdditive);
	//	eHose.getParticleNode().setQueueBucket(RenderQueue.Bucket.Transparent);
	//	main.getRootNode().addControl(eHose);
	}
	
	float hRatio, vRatio;
	float baseAngle = 90;
	float hAngle, vAngle;
	float[] angles = new float[3];
	boolean active = false;
	boolean flagForRemoval = false;
	
	public void activateHose(boolean active) {
		if (active) {
			if (!this.active)
				main.getRootNode().addControl(eHose);
			this.active = active;
			flagForRemoval = false;
			eHose.setEmissionsPerSecond(200);
		//	eHose.reset();
			eHose.setEnabled(true);
			main.getAudioManager().playLoopedAudio(FXKey.WATER);
		} else {
			eHose.setEmissionsPerSecond(0);
			main.getAudioManager().stopLoopedAudio(FXKey.WATER);
			flagForRemoval = true;
		}
	}
	
	public boolean isActive() { return this.active; }
	public boolean isFlaggedForRemoval() { return this.flagForRemoval; }
	
	@Override
	public void update(float tpf) {
		if (active) {
			// Horizontal offset
			hRatio = screen.getMouseXY().x/screen.getWidth();
			hAngle = -baseAngle;
			hAngle *= hRatio;
			hAngle += (baseAngle/2+5);
			hAngle *= FastMath.DEG_TO_RAD;
			
			// Vertical offset
			vRatio = screen.getMouseXY().y/screen.getHeight();
			eHose.setForceMinMax(4.0f+(16f*vRatio), 8f+(20f*vRatio));
			
			vAngle = -90;
			vAngle *= FastMath.DEG_TO_RAD;
			
			angles[0] = vAngle;
			angles[1] = hAngle;
			angles[2] = 0;
			eHose.setLocalRotation(q.fromAngles(angles));
			eHose.setLocalTranslation(main.getCamera().getLocation().add(1, -1, 0));
			
			if (flagForRemoval) {
				if (eHose.getActiveParticleCount() <= 0) {
					this.active = false;
					eHose.setEnabled(false);
					main.getRootNode().removeControl(eHose);
				}
			}
			
		}
	}
}
